package com.tjf.lib_core.action;

import android.os.CountDownTimer;
import android.widget.TextView;

/**
 * @author: tjf
 * @date: 2023-01-09
 * @desc： 倒计时
 */
public class CountDownTimesAction {

    static CountDownTimer mCountDownTimer = null;

    public static void countDownStart(int totalTimes, TextView timeView, CountDownTimerLiniser timerLiniser) {
        stopCountDownCancel();
        mCountDownTimer = new CountDownTimer((totalTimes + 1) * 1000L,
                1000L) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeView.setText("");
                int time = (int) (millisUntilFinished / 1000) + 1;
                timeView.setText(new StringBuffer(String.valueOf(time)).append("s"));
            }

            @Override
            public void onFinish() {
                if (timerLiniser != null) {
                    timerLiniser.CountDownTimerFinish();
                }
            }
        };
        mCountDownTimer.start();

    }


    public static void stopCountDownCancel() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }

    public interface CountDownTimerLiniser {
        void CountDownTimerFinish();
    }
}
